/*
Given a dictionary of strings [ strings are in sorted order] you have to find the precedence of characters according to the dictionary..
eat
bxy
e is ranked above b according to the dictionary.
*/

#include <vector>
#include <list>
#include <map>
#include <set>
#include <queue>
#include <deque>
#include <stack>
#include <bitset>
#include <algorithm>
#include <functional>
#include <numeric>
#include <utility>
#include <sstream>
#include <iostream>
#include <iomanip>
#include <cstdio>
#include <cmath>
#include <cstdlib>
#include <ctime>
#include <fstream>
#include <set>

using namespace std;


vector<char> topSort(vector<string> &str)
{
    map<char, set<char> > mp;
    set<char> charSet;
    for (int i = 0; i < str.size(); i++) {
        charSet.insert(str[i][0]);
        for (int j = 1; j < str[i].size(); j++) {
	    mp[str[i][j-1]].insert(str[i][j]);
	    charSet.insert(str[i][j]);
	}
    }
    vector<char> ans;
    int size = charSet.size();
    while(ans.size() < size) {
        for(set<char>::iterator  iter = charSet.begin(); iter != charSet.end(); iter++) {
            //cout << *iter << " " << mp.count(*iter) << " " << mp[*iter].size() << endl;
           // getchar();
	    if (mp.count(*iter) == 0 || mp[*iter].size() == 0) {
		ans.push_back(*iter);
                charSet.erase(*iter);
                for (map<char, set<char> >::iterator iter1 = mp.begin(); iter1 != mp.end(); iter1++) {
		    iter1->second.erase(*iter);
		}
	    }
	}
    }
    return ans;
}

int main(int argc, char **argv)
{
    vector<string> str;
    str.push_back("cat");
    str.push_back("tdi");
    str.push_back("ixzy");
    str.push_back("ijkl");
    vector<char> ans = topSort(str);
    for (int i = 0; i < ans.size(); i++) {
        cout << ans[i] << " ";
    }
}
